<?php


namespace Biz\service\advertisement\position;

use Biz\model\advertisement\MHhzAdmin;
use Biz\model\advertisement\MPositionAdType;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;

class BizPositionPuckProject extends BizPositionPageTpl
{
    const TPL_CONFIG_TPL_NAME = '占位后台项目';
    private $total = 0;
    public function getList(): array
    {
        $condition = $this->getRequestCondition();
        $list = \Biz\service\adCalendar\AdPosition::projectListToAdmin($condition['admin'] ?? '', $start_time = '', $end_time = '', $department_id = 0, $condition['name'] ?? '', $status=  -1, $this->getPage(), $this->getPageSize());
        $this->total = $list['total'] ?? 0;
        return $list['list'] ?? [];
    }

    public function getTotal(): int
    {
        return intval($this->total);
    }

    public function condition(): array
    {
        $name = $this->getRequest()->input('name', '');
        $admin = $this->getRequest()->input('admin', '');
        $condition = [];

        if ($name) $condition['name'] = $name;
        if ($admin) $condition['admin'] = $admin;

        return $condition;
    }



    public function getGrid(Grid $grid)
    {
        $grid->disableActions();
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('admin', '创建人')->multipleSelect($this->nameOptions('username'));
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('name', '项目名称');
            });
            if ($this->getRequestCondition()) $filter->expand();
        });
        $grid->column('id', 'ID');
        $grid->column('project_name', '项目名称');
        $grid->column('start_date', '开始时间');
        $grid->column('end_date', '结束时间');
        $grid->column('status_desc', '状态');
        $grid->column('admin', '操作人');
        $grid->column('department', '部门');
    }

    private function nameOptions($key = 'id')
    {
        $admin = MHhzAdmin::getAll();
        return $admin ? array_column($admin, 'username', $key) : [];
    }
    public function getForm(Form $form)
    {
        $options = $this->nameOptions();
        $form->hidden('id');
        $form->select('admin_id', '离职用户')->options($options)->required();
        $form->select('to_admin_id', '交接用户')->options($options)->required();
    }

    public function getFormInfo($id): array
    {
        return [];
    }

    /**
     * @throws Exception
     */
    public function save($input)
    {
        $admin_id = $input['admin_id'] ?? 0;
        $to_admin_id = $input['to_admin_id'] ?? 0;

        if ($admin_id && $to_admin_id) {
            $ids = \Biz\model\advertisement\MPositionProject::select('id', ['admin_id' => $admin_id]);
            \Biz\model\advertisement\MPositionProject::update(['admin_id' => $to_admin_id], ['id' => $ids]);
        } else {
            throw new Exception('离职用户和交接用户必须');
        }
    }

    public function delete($id): int
    {
        return 0;
    }


}